package com.songtech.server.celltrack.util;

import com.songtech.server.celltrack.model.CellTrack;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.HttpResponse;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;

/**
 * @Des 通过Url获取responseEntity
 * Created by wxq on 2018/4/17.
 */
public class TransUtil {

    /**
     * 基站定位：获取response中的entity
     * @param url 请求的地址
     * @param lac LAC编码
     * @param cell_id cell_id编码
     * @return 将entity中的内容写到StringBuilder中返回
     * @throws IOException
     */
    public static StringBuffer getContent(String url,String lac,String cell_id) throws IOException {

        // 生成一个httpclient对象,模拟http请求
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url + "?lac=" + lac + "&cell_id=" + cell_id + "&hex=false&flag=2262433451");
        httpPost.setHeader("Accept:","*/*");
        httpPost.setHeader("Accept-Encoding:","gzip, deflate");
        httpPost.setHeader("Connection:","keep-alive");
        httpPost.setHeader("Origin:","http://www.cellid.cn");
        httpPost.setHeader("Referer:","http://www.cellid.cn/m/index.php");//Referer:很多网站如果我们直接访问可能会报非法访问，一般就是这个参数不存在引起的
        HttpResponse response = httpclient.execute(httpPost);
        InputStream fis = response.getEntity().getContent();

        //将内容写到StringBuilder中
        BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
        StringBuffer sb = new StringBuffer();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        fis.close();

        return sb;
    }
//
//    /**
//     * 调用百度API转换经纬度
//     * @param longitude 经度
//     * @param latitude 纬度
//     * @return CellTrack实体：getLongitude --> 经度
//     *                         getLatitude --> 纬度
//     * @throws IOException
//     */
//    public static CellTrack baiduAPI(double longitude,double latitude) throws IOException{
//
//        String ak = "cHhRdQtrCPU59fVfYfv6mRXHT5eFrGCN";
//
//        CloseableHttpClient httpclient = HttpClients.createDefault();
//        HttpPost httpPost = new HttpPost("http://api.map.baidu.com/geoconv/v1/?coords=" + longitude + "," + latitude + "&from=1&to=5&ak=" + ak);
//        HttpResponse response = httpclient.execute(httpPost);
//        InputStream fis = response.getEntity().getContent();
//
//        //将内容写到StringBuilder中
//        BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
//        StringBuilder sb = new StringBuilder();
//        String line = null;
//        while ((line = reader.readLine()) != null) {
//            sb.append(line + "\n");
//        }
//        fis.close();
//
//        int a = sb.indexOf("x") + 3;
//        int b = sb.lastIndexOf("}]");
//
//        String str = sb.substring(a,b);
//        int c = str.lastIndexOf(",");
//        int d = c + 5;
//        String lon = str.substring(0,c);
//        String lat = str.substring(d);
//
//        CellTrack cellTrack = new CellTrack();
//        cellTrack.setLongitude(new BigDecimal(lon));
//        cellTrack.setLatitude(new BigDecimal(lat));
//        return cellTrack;
//    }

    /**
     * 通过算法转换经纬度(谷歌地图经纬度-->百度地图经纬度)
     * @param longitude 经度
     * @param latitude 纬度
     * @return CellTrack实体：getLongitude --> 经度
     *                         getLatitude --> 纬度
     */
    public static CellTrack getNewLongitudeAndLatitude(double longitude,double latitude){

        double PI = 3.14159265358979324;
        double X_PI = PI * 3000.0 / 180.0;
        double x = longitude;
        double y = latitude;
        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * X_PI);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * X_PI);
        longitude = z * Math.cos(theta) + 0.0065;
        latitude = z * Math.sin(theta) + 0.006;
        CellTrack cellTrack = new CellTrack();
        cellTrack.setLongitude(new BigDecimal(longitude));
        cellTrack.setLatitude(new BigDecimal(latitude));
        return cellTrack;
    }


    /**
     * 通过算法转换经纬度(谷歌地图经纬度-->百度地图经纬度)
     * @param longitude 经度
     * @param latitude 纬度
     * @return CellTrack实体：getLongitude --> 经度
     *                         getLatitude --> 纬度
     */

    public static CellTrack GGEarthMap2BDMap(double longitude,double latitude){

        double PI = Math.PI;

        //WGS-84 to GCJ-02
        double factor = 6378245; //  a: 卫星椭球坐标投影到平面地图坐标系的投影因子。
        double ee = 0.00669342162296594323; //  ee: 椭球的偏心率。
        double dLat = transLat(longitude - 105.0, latitude - 35.0);
        double dLon = transLon(longitude - 105.0, latitude - 35.0);
        double radLat = latitude / 180.0 * PI;
        double magic = Math.sin(radLat);
        magic = 1 - ee * magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        dLat = latitude + (dLat * 180.0) / ((factor * (1 - ee)) / (magic * sqrtMagic) * PI);
        dLon = longitude + (dLon * 180.0) / (factor / sqrtMagic * Math.cos(radLat) * PI);


        //GCJ-02 to BD-09
        CellTrack cellTrack = new CellTrack();
        double X_PI = PI * 3000.0 / 180.0;
//        double x = longitude;
//        double y = latitude;
        double x = dLon;
        double y = dLat;
        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * X_PI);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * X_PI);

        double newlon = z * Math.cos(theta) + 0.0065;
        double newlat = z * Math.sin(theta) + 0.006;


        //获取格式化对象
        NumberFormat format = NumberFormat.getNumberInstance();

        format.setMaximumFractionDigits(7);

        //设置四舍五入
        format.setRoundingMode(RoundingMode.UP);


        cellTrack.setLongitude(new BigDecimal(format.format(newlon)));
        cellTrack.setLatitude(new BigDecimal(format.format(newlat)));
        return cellTrack;
    }

//    //WGS-84 to GCJ-02
    private static CellTrack trans2GCJ02(CellTrack cellTrack){

        //    var a = 6378245.0; //  a: 卫星椭球坐标投影到平面地图坐标系的投影因子。
//    var ee = 0.00669342162296594323; //  ee: 椭球的偏心率。
//    var dLat = this.transformLat(lon - 105.0, lat - 35.0);
//    var dLon = this.transformLon(lon - 105.0, lat - 35.0);
//    var radLat = lat / 180.0 * this.PI;
//    var magic = Math.sin(radLat);
//    magic = 1 - ee * magic * magic;
//    var sqrtMagic = Math.sqrt(magic);
//    dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * this.PI);
//    dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * this.PI);

        return null;
    }


    private static double transLat(double x, double y){
        double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * Math.PI) + 20.0 * Math.sin(2.0 * x * Math.PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(y * Math.PI) + 40.0 * Math.sin(y / 3.0 * Math.PI)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(y / 12.0 * Math.PI) + 320 * Math.sin(y * Math.PI / 30.0)) * 2.0 / 3.0;
        return ret;
    }

    private static double transLon(double x, double y){
        double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * Math.PI) + 20.0 * Math.sin(2.0 * x * Math.PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(x * Math.PI) + 40.0 * Math.sin(x / 3.0 * Math.PI)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(x / 12.0 * Math.PI) + 300.0 * Math.sin(x / 30.0 * Math.PI)) * 2.0 / 3.0;
        return ret;
    }

}
